Skip to content

ext/dom: Return numeric results from XPath callbacks as numbers #22105

Draft
afflerbach wants to merge 1 commit into
php:masterfrom
afflerbach:xpath-callback-return-numbers
Draft

ext/dom: Return numeric results from XPath callbacks as numbers #22105
afflerbach wants to merge 1 commit into
php:masterfrom
afflerbach:xpath-callback-return-numbers

Conversation

@afflerbach
Copy link
Copy Markdown
Contributor

@afflerbach afflerbach force-pushed the xpath-callback-return-numbers branch from 8388fe6 to dc37660 Compare May 21, 2026 08:20
@devnexen
Copy link
Copy Markdown
Member

nice additions, want to see a bit more in the tests area, I ll write few remarks shortly.

<?php
$dom = Dom\XMLDocument::createFromString('<root/>');
$xpath = new Dom\XPath($dom);
$xpath->registerPhpFunctionNs('urn:x', 'get-int', fn(): int => 42);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • you should also test edge cases such as NAN, PHP_INT_MIN/MAX ...
  • Using XSLTProcessor::registerPHPFunctions for returning an int/float to assert proper values even as strings.
  • demonstrating benefits of your changes through an expression, e.g. x:get-int() + 1

Comment thread ext/dom/xpath_callbacks.c
} else if (Z_TYPE(callback_retval) == IS_FALSE || Z_TYPE(callback_retval) == IS_TRUE) {
valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(callback_retval) == IS_TRUE));
} else if (Z_TYPE(callback_retval) == IS_LONG) {
valuePush(ctxt, xmlXPathNewFloat(Z_LVAL(callback_retval)));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a BC break thus need a good UPGRADING entry.

Copy link
Copy Markdown
Member

@ndossche ndossche May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will break for numbers > 2**52 on 64-bit systems. Previously, it was represented as a string and could be coerced back to a php int without precision loss. Doubles can't reliably store integers > 2**52 so you would get precision loss.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what tests are for ;)

@afflerbach afflerbach marked this pull request as draft May 21, 2026 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants